import axios from 'axios';
import constants from './constants';

const getToken = function() {
    return window.localStorage.getItem(constants.ACCESS_TOKEN) || "";
}

axios.defaults.baseURL = `${process.env.VUE_APP_CKZL_TEST}/api`;
// axios.defaults.withCredentials = true;

axios.interceptors.request.use(
    config => {
        config.headers[constants.ACCESS_TOKEN] = getToken();
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        console.log("结果",response)
        return response.data;
    },
    err => {
        if (err) {
            let data = err.response.data
            switch (data.code) {
                case 400:
                    console.log("没执行？")
                    break;
                case 403:
                    // Message.error("您没有权限或暂未登录！")
                    break;
                case 500:
                    break;
                case 404:
                    break;
                case 504:
                    break;
                case 401:
                    // Message.error("您没有权限或暂未登录！")
                    break
                default:
                    console.error({
                        duration: 0,
                        forbidClick: true,
                        message: data.message
                    });
                    break;
            }
        }
        return err;
    }
)

export default axios;
